package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
	"strconv"
)

// 获取打卡日志信息
func GetMfcardUploadLogInfo(p map[string]interface{}) *models.MfcardUploadLog {
	o := orm.NewOrm()
	mfcardUploadLog := &models.MfcardUploadLog{}
	qs := o.QueryTable("MfcardUploadLog")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	qs.One(mfcardUploadLog)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if mfcardUploadLog.Id > 0 {
			mfcardUploadLog.DirectionShow = models.MfcardUploadLogDirection[mfcardUploadLog.Direction]
			mfcardUploadLog.CreatedShow = mfcardUploadLog.Created.Format("2006-01-02 15:04:05")
		}
	}
	return mfcardUploadLog
}

// 添加打卡日志
func AddMfcardUploadLog(input url.Values) (int64, error) {
	o := orm.NewOrm()
	mfcardUploadLog := &models.MfcardUploadLog{}
	device := &models.Device{}
	if _, ok := input["device_id"]; ok {
		device.Id = utils.Atoi64(input["device_id"][0])
		o.Read(device)
	}
	mfcardUploadLog.Device = device
	school := &models.School{}
	if _, ok := input["school_id"]; ok {
		school.Id = utils.Atoi64(input["school_id"][0])
		o.Read(school)
	}
	mfcardUploadLog.School = school
	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	mfcardUploadLog.Student = student
	if _, ok := input["device_sn"]; ok {
		mfcardUploadLog.DeviceSn = input["device_sn"][0]
	}
	if _, ok := input["school_sn"]; ok {
		mfcardUploadLog.SchoolSn = input["school_sn"][0]
	}
	if _, ok := input["card_num"]; ok {
		mfcardUploadLog.CardNum = input["card_num"][0]
	}
	if _, ok := input["direction"]; ok {
		mfcardUploadLog.Direction, _ = strconv.Atoi(input["direction"][0])
	}
	if _, ok := input["temp"]; ok {
		mfcardUploadLog.Temp = utils.Atof64(input["temp"][0])
	}
	return o.Insert(mfcardUploadLog)
}

// 获取打卡日志列表
func GetMfcardUploadLogList(p map[string]interface{}) (int64, []*models.MfcardUploadLog) {
	var mfcardUploadLogList []*models.MfcardUploadLog
	o := orm.NewOrm()
	qs := o.QueryTable("MfcardUploadLog")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["schoolName"]; ok {
		qs = qs.Filter("School__Name__contains", p["schoolName"])
	}
	if _, ok := p["studentName"]; ok {
		qs = qs.Filter("Student__RealName__contains", p["studentName"])
	}
	if _, ok := p["startTime"]; ok {
		qs = qs.Filter("created__gt", p["startTime"])
	}
	if _, ok := p["endTime"]; ok {
		qs = qs.Filter("created__lt", p["endTime"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&mfcardUploadLogList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, mfcardUploadLog := range mfcardUploadLogList {
			mfcardUploadLog.DirectionShow = models.MfcardUploadLogDirection[mfcardUploadLog.Direction]
			mfcardUploadLog.CreatedShow = mfcardUploadLog.Created.Format("2006-01-02 15:04:05")
		}
	}
	return totalCount, mfcardUploadLogList
}
